package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Api(tags = "订单相关接口")
@RequestMapping("/admin/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 条件搜索订单
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("条件搜索订单")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO dto) {
        log.info("条件搜索订单:{}", dto);
        PageResult pageResult = ordersService.conditionSearch(dto);
        return Result.success(pageResult);
    }




    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result statistics() {
        log.info("各个状态的订单数量统计");
        OrderStatisticsVO vo = ordersService.statistics();
        return Result.success(vo);
    }

    /**
     * 管理端查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("管理端查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        log.info("管理端查询订单详情:{}", id);
        OrderVO orderVO = ordersService.orderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 开始接单业务
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("开始接单业务")
    public Result<OrderStatisticsVO> orderReceiving(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("开始接单业务:{}", ordersConfirmDTO);
        OrderStatisticsVO orderStatisticsVO = ordersService.orderReceiving(ordersConfirmDTO);
        return Result.success(orderStatisticsVO);
    }

    /**
     * 开始拒单业务
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("开始拒单业务")
    public Result<OrderStatisticsVO> orderRejecting(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("开始拒单业务:{}", ordersRejectionDTO);
        OrderStatisticsVO orderStatisticsVO = ordersService.orderRejecting(ordersRejectionDTO);
        return Result.success(orderStatisticsVO);
    }

    @ApiOperation("接單")
    @PutMapping("/confirm/{id}")
    public Result confirm(@PathVariable("id") Long id) {
        log.info("接單:{}", id);
        ordersService.confirm(id);
        return Result.success();
    }
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result refuse(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消訂單：{}",ordersCancelDTO);
        ordersService.cancelByIds(ordersCancelDTO);
        return Result.success();
    }

    @ApiOperation("派送中")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable("id") Long id) {
        log.info("派送中:{}", id);
        ordersService.delivery(id);
        return Result.success();
    }

    @ApiOperation("完成訂單")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable("id") Long id) {
        log.info("完成訂單:{}", id);
        ordersService.repetitions(id);
        return Result.success();
    }

}
